import { ref, onMounted, reactive } from "vue";
import { getStats } from "@/api/system";
import { isAllEmpty } from "@pureadmin/utils";

export interface StatsCat {
  title: string;
  child: Stats[];
}
export interface Stats {
  title: string;
  value: string | [Stats];
  type?: number;
}

export function useColumns(options: any) {
  const dataList = ref<StatsCat[]>([]);
  const loading = ref(true);

  function loadData() {
    return new Promise((resolve, reject) => {
      getStats()
        .then(res => {
          const data = res.data;
          const sumData = data.sumData;
          const sumBean: StatsCat = {
            title: "汇总数据",
            child: [
              { title: "销售总业绩", value: sumData.total_sales_amount },
              { title: "PV产生的奖金", value: sumData.pv_generated_bonus },
              { title: "释放值总额", value: sumData.total_release_value },
              { title: "今日释放值", value: sumData.today_release_value },
              { title: "待释放值", value: sumData.pending_release_value },
              {
                title: "今日释放总收益",
                value: sumData.today_release_total_income
              },
              { title: "总释放收益", value: sumData.total_release_income },
              {
                title: "今日等级流量收益",
                value: sumData.today_rank_traffic_income
              },
              {
                title: "总等级流量收益",
                value: sumData.total_rank_traffic_income
              },

              {
                title: "总平级流量收益",
                value: sumData.total_same_level_traffic_income
              },
              {
                title: "今日流水收益",
                value: sumData.today_order_income
              }
            ]
          };
          const powerData = data.powerData;
          const powerBean: StatsCat = {
            title: "算力统计",
            child: [
              { title: "算力销售数量", value: powerData.power_count },
              { title: "累计销售额", value: powerData.total_sales_amount },
              { title: "今日新增销售额", value: powerData.today_sales_amount },
              { title: "算力总产出", value: powerData.total_output_power },
              { title: "算力已产出", value: powerData.output_power_so_far }
            ]
          };
          const orderData = data.orderData;
          const orderBean: StatsCat = {
            title: "商城统计",
            child: [
              { title: "订单总数", value: orderData.total_orders },
              { title: "今日销售总数", value: orderData.today_count },
              { title: "已处理订单数", value: orderData.processed_orders },
              { title: "待处理订单数", value: orderData.pending_orders }
            ]
          };
          const walletData = data.walletData;
          const walletBean: StatsCat = {
            title: "钱包统计",
            child: [
              { title: "今日新增提现金额", value: data.todayOut },
              {
                title: "已提现总金额",
                value: walletData.total_withdrawn_amount
              },
              {
                title: "可提现金额",
                value: walletData.available_withdrawal_amount
              },
              {
                title: "待处理提现金额",
                value: walletData.pending_withdrawals_amount
              },
              {
                title: "待处理提现笔数",
                value: walletData.pending_withdrawals_count
              }
            ]
          };
          const userData = data.userData;
          const userBean: StatsCat = {
            title: "用户统计",
            child: [
              { title: "注册用户总数", value: userData.total_users },
              { title: "昨日新增用户数", value: userData.new_users_yesterday },
              { title: "用户剩余金额", value: userData.user_money },
              { title: "用户剩余待领取收益", value: userData.user_score },
              { title: "用户剩余待领取收益(实时) ", value: data.userUnclaimed },
              {
                title: "流量收益",
                value: sumData.today_same_level_traffic_income
              }
              // {
              //   title: "用户等级人数",
              //   value: levelData,
              //   type: 1
              // }
            ]
          };
          const deviceData = data.deviceData;
          const deviceTitles = {
            1: "云盒统计",
            3: "水机统计",
            4: "充电宝统计"
          };
          const deviceBeans: StatsCat[] = deviceData.map((item: any) => {
            return {
              title: deviceTitles[item.device_type],
              child: [
                { title: "销售台数", value: item.sale_count },
                { title: "累计销售额", value: item.total_sales_amount },
                { title: "昨日新销售台数", value: item.today_sales_count },
                { title: "昨日新增销售额", value: item.today_sales_amount },
                { title: "待签约数量", value: item.pending_contracts_count }
              ]
            };
          });

          let levelData: Stats[] = [];
          if (!isAllEmpty(userData.user_level)) {
            levelData = Object.entries(JSON.parse(userData.user_level))
              .map(([key, value]) => ({
                title: key.replace("level", "V"),
                value: `(${value})人`
              }))
              .sort((a, b) => {
                return a.title.localeCompare(b.title);
              });
            // levelData.pop(); // 移除最后一项
          }

          ///
          const results = [
            sumBean,
            ...deviceBeans,
            powerBean,
            orderBean,
            walletBean,
            userBean
          ];
          if (options.userLevel) {
            console.log("options.userLevel", options);
            console.log("levelData", levelData);
            results.push({
              title: "用户等级人数",
              child: levelData
            });
          }
          dataList.value = results;
          loading.value = false;
          resolve(res.data);
        })
        .catch(error => {
          reject(error);
        });
    });
  }
  onMounted(() => {
    loadData();
  });
  return {
    loading,
    dataList
  };
}
